package com.qianbao.qianbaobusiness.ui.deposit;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baseandroid.config.Global;
import com.baseandroid.mvp.BaseMvpActivity;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.ui.support.customermanager.CustomerManagerActivity;
import com.qianbao.qianbaobusiness.ui.widget.dialog.ConfirmDialog;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

public class IntroducedServiceActivity extends BaseMvpActivity<IntroducedServiceView, IntroducedServicePresent> {
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbar_back;   //返回键
    @BindView(R.id.toolbar_title)
    TextView       toolbar_title;    //标题

    @BindView(R.id.tv_connect)
    TextView tvConnect;    //联系客户经理

    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
    private Intent        it;
    private ConfirmDialog customDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_service;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        toolbar_title.setText(IntroducedServiceActivity.this.getResources().getString(R.string.qianbao_introduce));
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick({R.id.toolbar_back, R.id.tv_connect})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                finish();
                break;

            case R.id.tv_connect:  //联系客户经理
                if (Global.getShopListInfo().merchantList.size() > 1) {
                    startActivity(new Intent(IntroducedServiceActivity.this, CustomerManagerActivity.class));
                } else {
                    ConfirmDialog.Builder builder = new ConfirmDialog.Builder(IntroducedServiceActivity.this);
                    builder.setContentText(Global.getShopListInfo().merchantList.get(0).managerMobile);
                    builder.setConfirmListener(new ConfirmDialog.Builder.ConfirmClickListener() {
                        @Override
                        public void click(boolean b) {
                            if (customDialog.isShowing()) {
                                customDialog.dismiss();
                            }
                            if (b) {
                                intentToCall(IntroducedServiceActivity.this, Global.getShopListInfo().merchantList.get(0).managerMobile);
                            }
                        }
                    });
                    customDialog = builder.create();
                    customDialog.show();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 直接拨打电话
     */
    public void intentToCall(final Context context, final String phoneNumber) {
        if (context == null || TextUtils.isEmpty(phoneNumber)) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startActivity(it);
            } else {
                // Permission Denied
                Toast.makeText(IntroducedServiceActivity.this, "请打开拨打电话权限", Toast.LENGTH_SHORT).show();
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
